package com.cn.kehong.service;


import com.cn.kehong.api.IPhysicalChemicalGranularityService;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PhysicalChemicalGranularityServiceImpl implements IPhysicalChemicalGranularityService {

    @Autowired
    private PhysicaChemicalGranularityMapper physicaChemicalGranularityMapper;
    @Autowired
    private PhysicalChemicalMapper physicalChemicalMapper;
    @Override
    public String addPhysicalGranularity(PhysicaChemicalGranularity physicaChemicalGranularity) throws Exception {
        if (null == physicaChemicalGranularity){
            return "请输入粒度检测信息";
        }
        String result = this.physicalGranularityChange(physicaChemicalGranularity);
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        String id = CommonUtil.generateRandomNum("granularity_");
        physicaChemicalGranularity.setId(id);
        physicaChemicalGranularity.setCreateTime(new Date());
        physicaChemicalGranularity.setCreateBy(physicaChemicalGranularity.getInspector());
        if (StringUtils.isNotEmpty(physicaChemicalGranularity.getPhysicalId())){
            PhysicalChemical physicalChemical = new PhysicalChemical();
            physicalChemical.setStatus("1");
            physicalChemical.setUpdateTime(new Date());
            physicalChemical.setSubmissionTime(new Date());
            physicalChemical.setUpdateBy(physicaChemicalGranularity.getCreateBy());
            PhysicalChemicalExample chemicalExample = new PhysicalChemicalExample();
            chemicalExample.createCriteria().andIdEqualTo(physicaChemicalGranularity.getPhysicalId());
            physicalChemicalMapper.updateByExampleSelective(physicalChemical,chemicalExample);
        }
        int count = physicaChemicalGranularityMapper.insertSelective(physicaChemicalGranularity);
        if (count < 1) {
            throw  new Exception("录入粒度检测信息失败！");
        }
        return CommonUtil.outStr(count);
    }

    private String physicalGranularityChange(PhysicaChemicalGranularity physicaChemicalGranularity) {
        String result = "";
        if (StringUtils.isBlank(physicaChemicalGranularity.getDetectionResult())){
            result = "请输入检测结果";
            return result;
        }
        return result;
    }

}
